extension MapExtension<K, V> on Map<K, V> {
  // 判空
  bool get isNullOrEmpty => isEmpty;
  bool get isNotNullOrEmpty => isNotEmpty;

  // 安全获取
  V? getOrNull(K key) => this[key];
  V getOrDefault(K key, V defaultValue) => this[key] ?? defaultValue;

  // 合并
  Map<K, V> merge(Map<K, V> other) {
    return {...this, ...other};
  }

  // 移除空值
  Map<K, V> removeNulls() {
    return Map.fromEntries(
      entries.where((entry) => entry.value != null),
    );
  }

  // 转换为查询字符串
  String toQueryString() {
    return entries
        .map((e) => '${Uri.encodeComponent(e.key.toString())}='
            '${Uri.encodeComponent(e.value.toString())}')
        .join('&');
  }
}
